﻿//  (c)2012 Henrik Nilsson
//  http://idmcrisis.com

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text.RegularExpressions;
using Microsoft.MetadirectoryServices;

namespace IdmCrisis.Fim.Connectors
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// Replaces Format strings between ? characters with a specified DateTime value.
        /// </summary>
        /// <see cref="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx"/>
        /// <param name="str">Input string</param>
        /// <param name="dateTime">The DateTime value to use for substitution.</param>
        /// <returns>A copy of input string in which any datetime format strings between ? characters are replaced by current datetime value.</returns>
        public static string SubstituteDateTimeFormat(this string str, DateTime dateTime)
        {
            str = Regex.Replace(str, @"\?(.*?)\?", m => dateTime.ToString(m.ToString().Replace("?", "")));
            return Regex.Replace(str, @"%3F(.*?)%3F", m => DateTime.Now.ToString(m.ToString().Replace("%3F", "")));
        }

        /// <summary>
        /// Replaces Format strings between '?' characters or if HTTP escaped '%3F' strings with the current datetime value.
        /// </summary>
        /// <see cref="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx"/>
        /// <param name="str">Input string</param>
        /// <returns>
        /// A copy of input string in which any datetime format strings between '?' characters 
        /// (and the HTTP equivalent '%3F' string) are replaced by current datetime value.
        /// </returns>
        public static string SubstituteDateTimeFormat(this string str)
        {
            return str.SubstituteDateTimeFormat(DateTime.Now);
        }

        /// <summary>
        /// Returns a byte array representation of a string.
        /// </summary>
        public static byte[] ToByteArray(this string str)
        {
            var bytes = new byte[str.Length * sizeof(char)];
            Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

        /// <summary>
        /// Gets the enum value corresponding to the string value.
        /// </summary>
        public static T ToEnum<T>(this string str)
        {
            return (T)Enum.Parse(typeof(T), str);
        } 

        /// <summary>
        /// Decrypts a SecureString to string
        /// </summary>
        public static string Decrypt(this SecureString secureString)
        {
            var ptr = Marshal.SecureStringToBSTR(secureString);
            var decrString = Marshal.PtrToStringUni(ptr);
            return string.IsNullOrEmpty(decrString) ? null : decrString;
        }


        /// <summary>
        /// Returns a Bool from a ConfigParameter.
        /// </summary>
        public static bool ToBool(this ConfigParameter configParameter)
        {
            return configParameter.Value != "0";
        }

        /// <summary>
        /// Returns a ByteArray from a ConfigParameter.
        /// </summary>
        public static byte[] ToByteArray(this ConfigParameter configParameter)
        {
            return configParameter.Value.ToByteArray();
        }

        /// <summary>
        /// Gets the enum value corresponding to the ConfigParameter value.
        /// </summary>
        public static T ToEnum<T>(this ConfigParameter configParameter)
        {
            return configParameter.Value.ToEnum<T>();
        }

        /// <summary>
        /// Checks if a string contains any of the characters in the char-array supplied.
        /// </summary>
        public static bool ContainsAny(this string str, char[] chars)
        {
            return str.IndexOfAny(chars) != -1;
        }

    }
}

